R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

library(dygraphs)
library(plotly)#inter-activity to any kind of plot
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(xts)#data time series
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.4     ✔ tibble    3.2.1
## ✔ purrr     1.0.4     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::first()  masks xts::first()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ dplyr::last()   masks xts::last()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dygraphs)
library(plotly)
library(xts)
library(tidyverse)
##sec 2 dygraphs(time series visualization)

##Example 1 simple time series
Nile_ts <-as.xts(Nile)
dygraph(Nile_ts,main = "Nile River Flow")
##
"INDIA-AQI-DATA-2015-2020.CSV" %>% 
  read_csv() %>% 
  select(c(Date,NO)) %>% 
  dygraph(main = "NO timeseries") %>% 
  dyRangeSelector()
## Rows: 29531 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (2): City, AQI_Bucket
## dbl  (13): PM2.5, PM10, NO, NO2, NOx, NH3, CO, SO2, O3, Benzene, Toluene, Xy...
## date  (1): Date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#example 2 add range selector
Nile_ts <-as.xts(Nile)
dygraph(Nile_ts,main = "Nile River Flow") %>% 
  dyRangeSelector()
#example 3
data <- cbind(mdeaths,fdeaths)
data_xts <- as.xts(data)
dygraph(data_xts,main = "Deaths in UK") %>% 
  dySeries("mdeaths",label = "male") %>% 
  dySeries("fdeaths",label = "female") %>% 
  dyOptions(colors = c("blue","red")) %>% 
  dyRangeSelector()
##EXAMPLE 4 customizing graphs

lungdeaths <- cbind(mdeaths,fdeaths)
dygraph(as.xts(lungdeaths),main = "Compairing deaths of male and female") %>% 
  dySeries("mdeaths",color = "darkblue") %>% 
  dySeries("fdeaths",color = "tomato") %>%
  dyHighlight(highlightCircleSize = 5,highlightSeriesBackgroundAlpha = 0.2) %>% 
  dyOptions(drawPoints = TRUE,pointSize = 2) %>% 
  dyLegend(show = "always")
lungdeaths <- cbind(mdeaths,fdeaths)
dygraph(as.xts(lungdeaths),main = "Compairing deaths of male and female") %>% 
  dySeries("mdeaths",color = "darkblue") %>% 
  dySeries("fdeaths",color = "tomato") %>%
  dyHighlight(highlightCircleSize = 5,highlightSeriesBackgroundAlpha = 0.2) %>% 
  dyOptions(drawPoints = TRUE,pointSize = 2) %>% 
  dyLegend(show = "auto") %>% #"auto","always","follow","never"
  dyRangeSelector()
 ##section2 plotly(interactive layer on ggplot)
  
  #Ex 1 :concert ggplot to plotly
  ggplot(mtcars, aes(x=wt , y=mpg , color= factor(cyl)))+
  geom_point(size=3)+
  labs(title = "Miles per Gallon vs Weight",
       x="Weight", y="MPG") -> plot_p
ggplotly(plot_p)
#Ex 2:interactive bar chat
plot_ly(data = mpg,x = ~class,type = "histogram")
##Ex 3 :line chart with hover info
plot_ly(data = economics, x = ~date, y = ~unemploy, type = 'scatter',
        mode = 'lines',
        line = list(color = 'purple')) %>%
  layout(title = "US Unemployment Over Time")
##Ex 4:Adding tooltips  and customization
plot_ly(mtcars, x = ~mpg,y= ~hp, type = 'scatter',mode = 'markers', 
        text = ~paste("car: ",rownames(mtcars),
                      "<br>MPG:",mpg,
                      "<br>HP: ",hp),
        marker = list(size = 10)) %>% 
  layout(title = "car horsepower vs MPG")